<template>
    <Table
        class="margin-15"
        :columns="getColumns"
        :data="getData"
        :loading="loading"
        border
    ></Table>
</template>
<script>
import {HttpDelAttach, HttpDelAttachNew } from '../../service/dictionary';
export default {
    props: {
        // 'data', 'wrapper', 'ajaxRequest', 'params',
        data: Array,
        columns: Array,
        // wrapper: Boolean,
        ajaxRequest: Function,
        params: Object,
        opActionMap: Object,
        type:String,
    },
    data() {
        return {
            dataList: [],
            loading: false
        };
    },
    created() {
        this.getLogList();
    },
    methods: {
        getLogList() {
            if (typeof this.ajaxRequest === "function") {
                this.loading = true;
                this.ajaxRequest(this.params).then(
                    res => {
                        this.loading = false;
                        this.dataList = res.data || [];
                    },
                    err => {
                        this.loading = false;
                        this.$Message.error(err.message);
                    }
                );
            }
        }
    },
    computed: {
        getData() {
            return this.data || this.dataList;
        },
        getColumns() {
            if (this.columns && this.columns.length > 0) return this.columns;
            return [
                {
                    title: '附件名称',
                    key: "attachTitle"
                },
                {
                    title: '附件链接',
                    key: "attachUrl"
                },
                {
                    title: '操作',
                    key: "opRemark",
                    render:(h, { row, index })=>{
                        let delButton = ''
                        if (this.type !== 'query') {
                            delButton = h('Button', {
                                props: {
                                    type: "error",
                                    size: 'small',
                                },
                                on: {
                                    click: () => {
                                        this.remove(index)
                                    }
                                },
                                style: {
                                    'margin-right': '15px'
                                }
                            }, '删除')
                        }
						return h('div', [
							delButton,
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.downfile(row.attachUrl)
                                    }
                                }
                            }, '下载')
                        ])
					}
                }
            ];
        }
    },
    methods:{
        downfile(url){
            this.handleWebfs(url, {}, 'download')
        },
        remove (index, useId) {
            // 新增时删除附件用另外的接口
            if (this.type === 'add') {
                HttpDelAttachNew({
                    id: useId ? index : this.datalist[index].attachId
                }).then(res => {
                    if (!useId) {   
                        this.datalist.splice(index, 1)
                        this.$Message.success(res.message)
                    }
                }, err => {
                    if (!useId) {
                        this.$Message.error(err.message)
                    }
                })
            }
            // 编辑
            else {
                HttpDelAttach({
                    id: this.id || 0,
                    attachId: useId ? index : this.datalist[index].attachId
                }).then(res => {
                    if (!useId) {
                        this.datalist.splice(index, 1)
                        this.$Message.success(res.message)
                    }
                }, err => {
                    if (!useId) {
                        this.$Message.error(err.message)
                    }
                })
            }
        },
    }
};
</script>
